<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveSupport::NumberHelper</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveSupport::NumberHelper 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activesupport/lib/active_support/number_helper_rb.html">activesupport/lib/active_support/number_helper.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>N</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="NumberHelper.html#method-i-number_to_currency">number_to_currency</a>,
              </li>
            
              
              <li>
                <a href="NumberHelper.html#method-i-number_to_delimited">number_to_delimited</a>,
              </li>
            
              
              <li>
                <a href="NumberHelper.html#method-i-number_to_human">number_to_human</a>,
              </li>
            
              
              <li>
                <a href="NumberHelper.html#method-i-number_to_human_size">number_to_human_size</a>,
              </li>
            
              
              <li>
                <a href="NumberHelper.html#method-i-number_to_percentage">number_to_percentage</a>,
              </li>
            
              
              <li>
                <a href="NumberHelper.html#method-i-number_to_phone">number_to_phone</a>,
              </li>
            
              
              <li>
                <a href="NumberHelper.html#method-i-number_to_rounded">number_to_rounded</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    
      <!-- Section constants -->
      <div class="sectiontitle">Constants</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class="attr-name">DEFAULTS</td>
            <td>=</td>
            <td class="attr-value">{
# Used in number_to_delimited
# These are also the defaults for 'currency', 'percentage', 'precision', and 'human'
format: {
# Sets the separator between the units, for more precision (e.g. 1.0 / 2.0 == 0.5)
separator: &quot;.&quot;,
# Delimits thousands (e.g. 1,000,000 is a million) (always in groups of three)
delimiter: &quot;,&quot;,
# Number of decimals, behind the separator (the number 1 with a precision of 2 gives: 1.00)
precision: 3,
# If set to true, precision will mean the number of significant digits instead
# of the number of decimal digits (1234 with precision 2 becomes 1200, 1.23543 becomes 1.2)
significant: false,
# If set, the zeros after the decimal separator will always be stripped (eg.: 1.200 will be 1.2)
strip_insignificant_zeros: false
},

# Used in number_to_currency
currency: {
format: {
format: &quot;%u%n&quot;,
negative_format: &quot;-%u%n&quot;,
unit: &quot;$&quot;,
# These five are to override number.format and are optional
separator: &quot;.&quot;,
delimiter: &quot;,&quot;,
precision: 2,
significant: false,
strip_insignificant_zeros: false
}
},

# Used in number_to_percentage
percentage: {
format: {
delimiter: &quot;&quot;,
format: &quot;%n%&quot;
}
},

# Used in number_to_rounded
precision: {
format: {
delimiter: &quot;&quot;
}
},

# Used in number_to_human_size and number_to_human
human: {
format: {
# These five are to override number.format and are optional
delimiter: &quot;&quot;,
precision: 3,
significant: true,
strip_insignificant_zeros: true
},
# Used in number_to_human_size
storage_units: {
# Storage units output formatting.
# %u is the storage unit, %n is the number (default: 2 MB)
format: &quot;%n %u&quot;,
units: {
byte: &quot;Bytes&quot;,
kb: &quot;KB&quot;,
mb: &quot;MB&quot;,
gb: &quot;GB&quot;,
tb: &quot;TB&quot;
}
},
# Used in number_to_human
decimal_units: {
format: &quot;%n %u&quot;,
# Decimal units output formatting
# By default we will only quantify some of the exponents
# but the commented ones might be defined or overridden
# by the user.
units: {
# femto: Quadrillionth
# pico: Trillionth
# nano: Billionth
# micro: Millionth
# mili: Thousandth
# centi: Hundredth
# deci: Tenth
unit: &quot;&quot;,
# ten:
#   one: Ten
#   other: Tens
# hundred: Hundred
thousand: &quot;Thousand&quot;,
million: &quot;Million&quot;,
billion: &quot;Billion&quot;,
trillion: &quot;Trillion&quot;,
quadrillion: &quot;Quadrillion&quot;
}
}
}
}</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">DECIMAL_UNITS</td>
            <td>=</td>
            <td class="attr-value">{ 0 =&gt; :unit, 1 =&gt; :ten, 2 =&gt; :hundred, 3 =&gt; :thousand, 6 =&gt; :million, 9 =&gt; :billion, 12 =&gt; :trillion, 15 =&gt; :quadrillion,
-1 =&gt; :deci, -2 =&gt; :centi, -3 =&gt; :mili, -6 =&gt; :micro, -9 =&gt; :nano, -12 =&gt; :pico, -15 =&gt; :femto }</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
          <tr valign='top'>
            <td class="attr-name">STORAGE_UNITS</td>
            <td>=</td>
            <td class="attr-value">[:byte, :kb, :mb, :gb, :tb]</td>
          </tr>
          
            <tr valign='top'>
              <td>&nbsp;</td>
              <td colspan="2" class="attr-desc"></td>
            </tr>
          
        
      </table>
    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-number_to_currency">
            
              <b>number_to_currency</b>(number, options = {})
            
            <a href="NumberHelper.html#method-i-number_to_currency" name="method-i-number_to_currency" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Formats a <code>number</code> into a currency string (e.g., $13.65). You
can customize the format in the <code>options</code> hash.</p>

<h4 id="method-i-number_to_currency-label-Options">Options</h4>
<ul><li>
<p><code>:locale</code> - Sets the locale to be used for formatting (defaults
to current locale).</p>
</li><li>
<p><code>:precision</code> - Sets the level of precision (defaults to 2).</p>
</li><li>
<p><code>:unit</code> - Sets the denomination of the currency (defaults to
"$").</p>
</li><li>
<p><code>:separator</code> - Sets the separator between the units (defaults to
".").</p>
</li><li>
<p><code>:delimiter</code> - Sets the thousands delimiter (defaults to ",").</p>
</li><li>
<p><code>:format</code> - Sets the format for non-negative numbers (defaults
to "%u%n").  Fields are <code>%u</code> for the currency, and
<code>%n</code> for the number.</p>
</li><li>
<p><code>:negative_format</code> - Sets the format for negative numbers
(defaults to prepending an hyphen to the formatted number given by
<code>:format</code>).  Accepts the same fields than <code>:format</code>,
except <code>%n</code> is here the absolute value of the number.</p>
</li></ul>

<h4 id="method-i-number_to_currency-label-Examples">Examples</h4>

<pre class="ruby"><span class="ruby-identifier">number_to_currency</span>(<span class="ruby-value">1234567890.50</span>)                <span class="ruby-comment"># =&gt; $1,234,567,890.50</span>
<span class="ruby-identifier">number_to_currency</span>(<span class="ruby-value">1234567890.506</span>)               <span class="ruby-comment"># =&gt; $1,234,567,890.51</span>
<span class="ruby-identifier">number_to_currency</span>(<span class="ruby-value">1234567890.506</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">3</span>) <span class="ruby-comment"># =&gt; $1,234,567,890.506</span>
<span class="ruby-identifier">number_to_currency</span>(<span class="ruby-value">1234567890.506</span>, <span class="ruby-identifier">locale</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">fr</span>)  <span class="ruby-comment"># =&gt; 1 234 567 890,51 €</span>
<span class="ruby-identifier">number_to_currency</span>(<span class="ruby-string">'123a456'</span>)                    <span class="ruby-comment"># =&gt; $123a456</span>

<span class="ruby-identifier">number_to_currency</span>(<span class="ruby-value">-1234567890.50</span>, <span class="ruby-identifier">negative_format</span><span class="ruby-operator">:</span> <span class="ruby-string">'(%u%n)'</span>)
<span class="ruby-comment"># =&gt; ($1,234,567,890.50)</span>
<span class="ruby-identifier">number_to_currency</span>(<span class="ruby-value">1234567890.50</span>, <span class="ruby-identifier">unit</span><span class="ruby-operator">:</span> <span class="ruby-string">'&amp;pound;'</span>, <span class="ruby-identifier">separator</span><span class="ruby-operator">:</span> <span class="ruby-string">','</span>, <span class="ruby-identifier">delimiter</span><span class="ruby-operator">:</span> <span class="ruby-string">''</span>)
<span class="ruby-comment"># =&gt; &amp;pound;1234567890,50</span>
<span class="ruby-identifier">number_to_currency</span>(<span class="ruby-value">1234567890.50</span>, <span class="ruby-identifier">unit</span><span class="ruby-operator">:</span> <span class="ruby-string">'&amp;pound;'</span>, <span class="ruby-identifier">separator</span><span class="ruby-operator">:</span> <span class="ruby-string">','</span>, <span class="ruby-identifier">delimiter</span><span class="ruby-operator">:</span> <span class="ruby-string">''</span>, <span class="ruby-identifier">format</span><span class="ruby-operator">:</span> <span class="ruby-string">'%n %u'</span>)
<span class="ruby-comment"># =&gt; 1234567890,50 &amp;pound;</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-number_to_currency_source')" id="l_method-i-number_to_currency_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e772daad20e865297f1907815b44f83de6f41f46/activesupport/lib/active_support/number_helper.rb#L201" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-number_to_currency_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/number_helper.rb, line 201</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">number_to_currency</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">number</span>
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>

  <span class="ruby-identifier">currency</span> = <span class="ruby-identifier">i18n_format_options</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:currency</span>)
  <span class="ruby-identifier">currency</span>[<span class="ruby-value">:negative_format</span>] <span class="ruby-operator">||=</span> <span class="ruby-string">&quot;-&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">currency</span>[<span class="ruby-value">:format</span>] <span class="ruby-keyword">if</span> <span class="ruby-identifier">currency</span>[<span class="ruby-value">:format</span>]

  <span class="ruby-identifier">defaults</span>  = <span class="ruby-identifier">default_format_options</span>(<span class="ruby-value">:currency</span>).<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">currency</span>)
  <span class="ruby-identifier">defaults</span>[<span class="ruby-value">:negative_format</span>] = <span class="ruby-string">&quot;-&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:format</span>] <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:format</span>]
  <span class="ruby-identifier">options</span>   = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">options</span>)

  <span class="ruby-identifier">unit</span>      = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:unit</span>)
  <span class="ruby-identifier">format</span>    = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:format</span>)

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_f</span>.<span class="ruby-identifier">phase</span> <span class="ruby-operator">!=</span> <span class="ruby-number">0</span>
    <span class="ruby-identifier">format</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-value">:negative_format</span>)
    <span class="ruby-identifier">number</span> = <span class="ruby-identifier">number</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-string">&quot;abs&quot;</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">abs</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">sub</span>(<span class="ruby-regexp">/^-/</span>, <span class="ruby-string">''</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">format</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-string">'%n'</span>, <span class="ruby-keyword">self</span>.<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span>)).<span class="ruby-identifier">gsub</span>(<span class="ruby-string">'%u'</span>, <span class="ruby-identifier">unit</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-number_to_delimited">
            
              <b>number_to_delimited</b>(number, options = {})
            
            <a href="NumberHelper.html#method-i-number_to_delimited" name="method-i-number_to_delimited" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Formats a <code>number</code> with grouped thousands using
<code>delimiter</code> (e.g., 12,324). You can customize the format in the
<code>options</code> hash.</p>

<h4 id="method-i-number_to_delimited-label-Options">Options</h4>
<ul><li>
<p><code>:locale</code> - Sets the locale to be used for formatting (defaults
to current locale).</p>
</li><li>
<p><code>:delimiter</code> - Sets the thousands delimiter (defaults to ",").</p>
</li><li>
<p><code>:separator</code> - Sets the separator between the fractional and
integer digits (defaults to ".").</p>
</li></ul>

<h4 id="method-i-number_to_delimited-label-Examples">Examples</h4>

<pre class="ruby"><span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-value">12345678</span>)                    <span class="ruby-comment"># =&gt; 12,345,678</span>
<span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-string">'123456'</span>)                    <span class="ruby-comment"># =&gt; 123,456</span>
<span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-value">12345678.05</span>)                 <span class="ruby-comment"># =&gt; 12,345,678.05</span>
<span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-value">12345678</span>, <span class="ruby-identifier">delimiter</span><span class="ruby-operator">:</span> <span class="ruby-string">'.'</span>)    <span class="ruby-comment"># =&gt; 12.345.678</span>
<span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-value">12345678</span>, <span class="ruby-identifier">delimiter</span><span class="ruby-operator">:</span> <span class="ruby-string">','</span>)    <span class="ruby-comment"># =&gt; 12,345,678</span>
<span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-value">12345678.05</span>, <span class="ruby-identifier">separator</span><span class="ruby-operator">:</span> <span class="ruby-string">' '</span>) <span class="ruby-comment"># =&gt; 12,345,678 05</span>
<span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-value">12345678.05</span>, <span class="ruby-identifier">locale</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">fr</span>)    <span class="ruby-comment"># =&gt; 12 345 678,05</span>
<span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-string">'112a'</span>)                      <span class="ruby-comment"># =&gt; 112a</span>
<span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-value">98765432.98</span>, <span class="ruby-identifier">delimiter</span><span class="ruby-operator">:</span> <span class="ruby-string">' '</span>, <span class="ruby-identifier">separator</span><span class="ruby-operator">:</span> <span class="ruby-string">','</span>)
<span class="ruby-comment"># =&gt; 98 765 432,98</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-number_to_delimited_source')" id="l_method-i-number_to_delimited_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e772daad20e865297f1907815b44f83de6f41f46/activesupport/lib/active_support/number_helper.rb#L291" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-number_to_delimited_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/number_helper.rb, line 291</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">number_to_delimited</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">number</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">valid_float?</span>(<span class="ruby-identifier">number</span>)

  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">format_options</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>]).<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">options</span>)

  <span class="ruby-identifier">parts</span> = <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">'.'</span>)
  <span class="ruby-identifier">parts</span>[<span class="ruby-number">0</span>].<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp">/(\d)(?=(\d\d\d)+(?!\d))/</span>, <span class="ruby-node">&quot;\\1#{options[:delimiter]}&quot;</span>)
  <span class="ruby-identifier">parts</span>.<span class="ruby-identifier">join</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:separator</span>])
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-number_to_human">
            
              <b>number_to_human</b>(number, options = {})
            
            <a href="NumberHelper.html#method-i-number_to_human" name="method-i-number_to_human" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Pretty prints (formats and approximates) a number in a way it is more
readable by humans (eg.: 1200000000 becomes “1.2 Billion”). This is useful
for numbers that can get very large (and too hard to read).</p>

<p>See <code>number_to_human_size</code> if you want to print a file size.</p>

<p>You can also define you own unit-quantifier names if you want to use other
decimal units (eg.: 1500 becomes “1.5 kilometers”, 0.150 becomes “150
milliliters”, etc). You may define a wide range of unit quantifiers, even
fractional ones (centi, deci, mili, etc).</p>

<h4 id="method-i-number_to_human-label-Options">Options</h4>
<ul><li>
<p><code>:locale</code> - Sets the locale to be used for formatting (defaults
to current locale).</p>
</li><li>
<p><code>:precision</code> - Sets the precision of the number (defaults to 3).</p>
</li><li>
<p><code>:significant</code> - If <code>true</code>, precision will be the #
of significant_digits. If <code>false</code>, the # of fractional digits
(defaults to <code>true</code>)</p>
</li><li>
<p><code>:separator</code> - Sets the separator between the fractional and
integer digits (defaults to ".").</p>
</li><li>
<p><code>:delimiter</code> - Sets the thousands delimiter (defaults to "").</p>
</li><li>
<p><code>:strip_insignificant_zeros</code> - If <code>true</code> removes
insignificant zeros after the decimal separator (defaults to
<code>true</code>)</p>
</li><li>
<p><code>:units</code> - A <a href="../Hash.html">Hash</a> of unit quantifier
names. Or a string containing an i18n scope where to find this hash. It
might have the following keys:</p>
<ul><li>
<p><strong>integers</strong>: <code>:unit</code>, <code>:ten</code>,
*<code>:hundred</code>, <code>:thousand</code>, <code>:million</code>,
*<code>:billion</code>, <code>:trillion</code>, *<code>:quadrillion</code></p>
</li><li>
<p><strong>fractionals</strong>: <code>:deci</code>, <code>:centi</code>,
*<code>:mili</code>, <code>:micro</code>, <code>:nano</code>,
*<code>:pico</code>, <code>:femto</code></p>
</li></ul>
</li><li>
<p><code>:format</code> - Sets the format of the output string (defaults to
"%n %u"). The field types are:</p>
<ul><li>
<p>%u - The quantifier (ex.: ‘thousand’)</p>
</li><li>
<p>%n - The number</p>
</li></ul>
</li></ul>

<h4 id="method-i-number_to_human-label-Examples">Examples</h4>

<pre class="ruby"><span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">123</span>)                         <span class="ruby-comment"># =&gt; &quot;123&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">1234</span>)                        <span class="ruby-comment"># =&gt; &quot;1.23 Thousand&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">12345</span>)                       <span class="ruby-comment"># =&gt; &quot;12.3 Thousand&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">1234567</span>)                     <span class="ruby-comment"># =&gt; &quot;1.23 Million&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">1234567890</span>)                  <span class="ruby-comment"># =&gt; &quot;1.23 Billion&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">1234567890123</span>)               <span class="ruby-comment"># =&gt; &quot;1.23 Trillion&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">1234567890123456</span>)            <span class="ruby-comment"># =&gt; &quot;1.23 Quadrillion&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">1234567890123456789</span>)         <span class="ruby-comment"># =&gt; &quot;1230 Quadrillion&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">489939</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">2</span>)        <span class="ruby-comment"># =&gt; &quot;490 Thousand&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">489939</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">4</span>)        <span class="ruby-comment"># =&gt; &quot;489.9 Thousand&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">1234567</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">4</span>,
                         <span class="ruby-identifier">significant</span><span class="ruby-operator">:</span> <span class="ruby-keyword">false</span>) <span class="ruby-comment"># =&gt; &quot;1.2346 Million&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">1234567</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">1</span>,
                         <span class="ruby-identifier">separator</span><span class="ruby-operator">:</span> <span class="ruby-string">','</span>,
                         <span class="ruby-identifier">significant</span><span class="ruby-operator">:</span> <span class="ruby-keyword">false</span>) <span class="ruby-comment"># =&gt; &quot;1,2 Million&quot;</span>
</pre>

<p>Non-significant zeros after the decimal separator are stripped out by
default (set <code>:strip_insignificant_zeros</code> to <code>false</code>
to change that):</p>

<pre class="ruby"><span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">12345012345</span>, <span class="ruby-identifier">significant_digits</span><span class="ruby-operator">:</span> <span class="ruby-value">6</span>) <span class="ruby-comment"># =&gt; &quot;12.345 Billion&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">500000000</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">5</span>)            <span class="ruby-comment"># =&gt; &quot;500 Million&quot;</span>
</pre>

<h4 id="method-i-number_to_human-label-Custom+Unit+Quantifiers">Custom Unit Quantifiers</h4>

<p>You can also use your own custom unit quantifiers:</p>

<pre class="ruby"><span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">500000</span>, <span class="ruby-identifier">units</span><span class="ruby-operator">:</span> { <span class="ruby-identifier">unit</span><span class="ruby-operator">:</span> <span class="ruby-string">'ml'</span>, <span class="ruby-identifier">thousand</span><span class="ruby-operator">:</span> <span class="ruby-string">'lt'</span> })  <span class="ruby-comment"># =&gt; &quot;500 lt&quot;</span>
</pre>

<p>If in your <a href="../I18n.html">I18n</a> locale you have:</p>

<pre>distance:
  centi:
    one: &quot;centimeter&quot;
    other: &quot;centimeters&quot;
  unit:
    one: &quot;meter&quot;
    other: &quot;meters&quot;
  thousand:
    one: &quot;kilometer&quot;
    other: &quot;kilometers&quot;
  billion: &quot;gazillion-distance&quot;</pre>

<p>Then you could do:</p>

<pre class="ruby"><span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">543934</span>, <span class="ruby-identifier">units</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">distance</span>)            <span class="ruby-comment"># =&gt; &quot;544 kilometers&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">54393498</span>, <span class="ruby-identifier">units</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">distance</span>)          <span class="ruby-comment"># =&gt; &quot;54400 kilometers&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">54393498000</span>, <span class="ruby-identifier">units</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">distance</span>)       <span class="ruby-comment"># =&gt; &quot;54.4 gazillion-distance&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">343</span>, <span class="ruby-identifier">units</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">distance</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">1</span>) <span class="ruby-comment"># =&gt; &quot;300 meters&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">1</span>, <span class="ruby-identifier">units</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">distance</span>)                 <span class="ruby-comment"># =&gt; &quot;1 meter&quot;</span>
<span class="ruby-identifier">number_to_human</span>(<span class="ruby-value">0.34</span>, <span class="ruby-identifier">units</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">distance</span>)              <span class="ruby-comment"># =&gt; &quot;34 centimeters&quot;</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-number_to_human_source')" id="l_method-i-number_to_human_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e772daad20e865297f1907815b44f83de6f41f46/activesupport/lib/active_support/number_helper.rb#L552" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-number_to_human_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/number_helper.rb, line 552</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">number_to_human</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">number</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">valid_float?</span>(<span class="ruby-identifier">number</span>)
  <span class="ruby-identifier">number</span> = <span class="ruby-constant">Float</span>(<span class="ruby-identifier">number</span>)

  <span class="ruby-identifier">defaults</span> = <span class="ruby-identifier">format_options</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:human</span>)
  <span class="ruby-identifier">options</span>  = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">options</span>)

  <span class="ruby-comment">#for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files</span>
  <span class="ruby-identifier">options</span>[<span class="ruby-value">:strip_insignificant_zeros</span>] = <span class="ruby-keyword">true</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">not</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-value">:strip_insignificant_zeros</span>)

  <span class="ruby-identifier">inverted_du</span> = <span class="ruby-constant">DECIMAL_UNITS</span>.<span class="ruby-identifier">invert</span>

  <span class="ruby-identifier">units</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span> <span class="ruby-value">:units</span>
  <span class="ruby-identifier">unit_exponents</span> = <span class="ruby-keyword">case</span> <span class="ruby-identifier">units</span>
  <span class="ruby-keyword">when</span> <span class="ruby-constant">Hash</span>
    <span class="ruby-identifier">units</span>
  <span class="ruby-keyword">when</span> <span class="ruby-constant">String</span>, <span class="ruby-constant">Symbol</span>
    <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value">:&quot;#{units}&quot;</span>, <span class="ruby-value">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>)
  <span class="ruby-keyword">when</span> <span class="ruby-keyword">nil</span>
    <span class="ruby-identifier">translate_number_value_with_default</span>(<span class="ruby-string">&quot;human.decimal_units.units&quot;</span>, <span class="ruby-value">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;:units must be a Hash or String translation scope.&quot;</span>
  <span class="ruby-keyword">end</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">map</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">e_name</span><span class="ruby-operator">|</span> <span class="ruby-identifier">inverted_du</span>[<span class="ruby-identifier">e_name</span>] }.<span class="ruby-identifier">sort_by</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">e</span>}

  <span class="ruby-identifier">number_exponent</span> = <span class="ruby-identifier">number</span> <span class="ruby-operator">!=</span> <span class="ruby-number">0</span> <span class="ruby-operator">?</span> <span class="ruby-constant">Math</span>.<span class="ruby-identifier">log10</span>(<span class="ruby-identifier">number</span>.<span class="ruby-identifier">abs</span>).<span class="ruby-identifier">floor</span> <span class="ruby-operator">:</span> <span class="ruby-number">0</span>
  <span class="ruby-identifier">display_exponent</span> = <span class="ruby-identifier">unit_exponents</span>.<span class="ruby-identifier">find</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">number_exponent</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-identifier">e</span> } <span class="ruby-operator">||</span> <span class="ruby-number">0</span>
  <span class="ruby-identifier">number</span>  <span class="ruby-operator">/=</span> <span class="ruby-number">10</span> <span class="ruby-operator">**</span> <span class="ruby-identifier">display_exponent</span>

  <span class="ruby-identifier">unit</span> = <span class="ruby-keyword">case</span> <span class="ruby-identifier">units</span>
  <span class="ruby-keyword">when</span> <span class="ruby-constant">Hash</span>
    <span class="ruby-identifier">units</span>[<span class="ruby-constant">DECIMAL_UNITS</span>[<span class="ruby-identifier">display_exponent</span>]] <span class="ruby-operator">||</span> <span class="ruby-string">''</span>
  <span class="ruby-keyword">when</span> <span class="ruby-constant">String</span>, <span class="ruby-constant">Symbol</span>
    <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-value">:&quot;#{units}.#{DECIMAL_UNITS[display_exponent]}&quot;</span>, <span class="ruby-value">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_i</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">translate_number_value_with_default</span>(<span class="ruby-node">&quot;human.decimal_units.units.#{DECIMAL_UNITS[display_exponent]}&quot;</span>, <span class="ruby-value">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_i</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">decimal_format</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:format</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">translate_number_value_with_default</span>(<span class="ruby-string">'human.decimal_units.format'</span>, <span class="ruby-value">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>])
  <span class="ruby-identifier">formatted_number</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">decimal_format</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp">/%n/</span>, <span class="ruby-identifier">formatted_number</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp">/%u/</span>, <span class="ruby-identifier">unit</span>).<span class="ruby-identifier">strip</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-number_to_human_size">
            
              <b>number_to_human_size</b>(number, options = {})
            
            <a href="NumberHelper.html#method-i-number_to_human_size" name="method-i-number_to_human_size" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Formats the bytes in <code>number</code> into a more understandable
representation (e.g., giving it 1500 yields 1.5 KB). This method is useful
for reporting file sizes to users. You can customize the format in the
<code>options</code> hash.</p>

<p>See <code>number_to_human</code> if you want to pretty-print a generic
number.</p>

<h4 id="method-i-number_to_human_size-label-Options">Options</h4>
<ul><li>
<p><code>:locale</code> - Sets the locale to be used for formatting (defaults
to current locale).</p>
</li><li>
<p><code>:precision</code> - Sets the precision of the number (defaults to 3).</p>
</li><li>
<p><code>:significant</code> - If <code>true</code>, precision will be the #
of significant_digits. If <code>false</code>, the # of fractional digits
(defaults to <code>true</code>)</p>
</li><li>
<p><code>:separator</code> - Sets the separator between the fractional and
integer digits (defaults to ".").</p>
</li><li>
<p><code>:delimiter</code> - Sets the thousands delimiter (defaults to "").</p>
</li><li>
<p><code>:strip_insignificant_zeros</code> - If <code>true</code> removes
insignificant zeros after the decimal separator (defaults to
<code>true</code>)</p>
</li><li>
<p><code>:prefix</code> - If <code>:si</code> formats the number using the SI
prefix (defaults to :binary)</p>
</li></ul>

<h4 id="method-i-number_to_human_size-label-Examples">Examples</h4>

<pre class="ruby"><span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">123</span>)                                    <span class="ruby-comment"># =&gt; 123 Bytes</span>
<span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">1234</span>)                                   <span class="ruby-comment"># =&gt; 1.21 KB</span>
<span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">12345</span>)                                  <span class="ruby-comment"># =&gt; 12.1 KB</span>
<span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">1234567</span>)                                <span class="ruby-comment"># =&gt; 1.18 MB</span>
<span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">1234567890</span>)                             <span class="ruby-comment"># =&gt; 1.15 GB</span>
<span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">1234567890123</span>)                          <span class="ruby-comment"># =&gt; 1.12 TB</span>
<span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">1234567</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">2</span>)                  <span class="ruby-comment"># =&gt; 1.2 MB</span>
<span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">483989</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">2</span>)                   <span class="ruby-comment"># =&gt; 470 KB</span>
<span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">1234567</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">2</span>, <span class="ruby-identifier">separator</span><span class="ruby-operator">:</span> <span class="ruby-string">','</span>)  <span class="ruby-comment"># =&gt; 1,2 MB</span>
</pre>

<p>Non-significant zeros after the fractional separator are stripped out by
default (set <code>:strip_insignificant_zeros</code> to <code>false</code>
to change that):</p>

<pre class="ruby"><span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">1234567890123</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">5</span>) <span class="ruby-comment"># =&gt; &quot;1.1229 TB&quot;</span>
<span class="ruby-identifier">number_to_human_size</span>(<span class="ruby-value">524288000</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">5</span>)     <span class="ruby-comment"># =&gt; &quot;500 MB&quot;</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-number_to_human_size_source')" id="l_method-i-number_to_human_size_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e772daad20e865297f1907815b44f83de6f41f46/activesupport/lib/active_support/number_helper.rb#L422" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-number_to_human_size_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/number_helper.rb, line 422</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">number_to_human_size</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">number</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">valid_float?</span>(<span class="ruby-identifier">number</span>)
  <span class="ruby-identifier">number</span> = <span class="ruby-constant">Float</span>(<span class="ruby-identifier">number</span>)

  <span class="ruby-identifier">defaults</span> = <span class="ruby-identifier">format_options</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:human</span>)
  <span class="ruby-identifier">options</span>  = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">options</span>)

  <span class="ruby-comment">#for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files</span>
  <span class="ruby-identifier">options</span>[<span class="ruby-value">:strip_insignificant_zeros</span>] = <span class="ruby-keyword">true</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">not</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">key?</span>(<span class="ruby-value">:strip_insignificant_zeros</span>)

  <span class="ruby-identifier">storage_units_format</span> = <span class="ruby-identifier">translate_number_value_with_default</span>(<span class="ruby-string">'human.storage_units.format'</span>, <span class="ruby-value">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>)

  <span class="ruby-identifier">base</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:prefix</span>] <span class="ruby-operator">==</span> <span class="ruby-value">:si</span> <span class="ruby-operator">?</span> <span class="ruby-number">1000</span> <span class="ruby-operator">:</span> <span class="ruby-number">1024</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">base</span>
    <span class="ruby-identifier">unit</span> = <span class="ruby-identifier">translate_number_value_with_default</span>(<span class="ruby-string">'human.storage_units.units.byte'</span>, <span class="ruby-value">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_i</span>, <span class="ruby-value">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>)
    <span class="ruby-identifier">storage_units_format</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp">/%n/</span>, <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_i</span>.<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp">/%u/</span>, <span class="ruby-identifier">unit</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">max_exp</span>  = <span class="ruby-constant">STORAGE_UNITS</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">-</span> <span class="ruby-number">1</span>
    <span class="ruby-identifier">exponent</span> = (<span class="ruby-constant">Math</span>.<span class="ruby-identifier">log</span>(<span class="ruby-identifier">number</span>) <span class="ruby-operator">/</span> <span class="ruby-constant">Math</span>.<span class="ruby-identifier">log</span>(<span class="ruby-identifier">base</span>)).<span class="ruby-identifier">to_i</span> <span class="ruby-comment"># Convert to base</span>
    <span class="ruby-identifier">exponent</span> = <span class="ruby-identifier">max_exp</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">exponent</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">max_exp</span> <span class="ruby-comment"># we need this to avoid overflow for the highest unit</span>
    <span class="ruby-identifier">number</span>  <span class="ruby-operator">/=</span> <span class="ruby-identifier">base</span> <span class="ruby-operator">**</span> <span class="ruby-identifier">exponent</span>

    <span class="ruby-identifier">unit_key</span> = <span class="ruby-constant">STORAGE_UNITS</span>[<span class="ruby-identifier">exponent</span>]
    <span class="ruby-identifier">unit</span> = <span class="ruby-identifier">translate_number_value_with_default</span>(<span class="ruby-node">&quot;human.storage_units.units.#{unit_key}&quot;</span>, <span class="ruby-value">:locale</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:count</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">number</span>, <span class="ruby-value">:raise</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword">true</span>)

    <span class="ruby-identifier">formatted_number</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span>)
    <span class="ruby-identifier">storage_units_format</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp">/%n/</span>, <span class="ruby-identifier">formatted_number</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp">/%u/</span>, <span class="ruby-identifier">unit</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-number_to_percentage">
            
              <b>number_to_percentage</b>(number, options = {})
            
            <a href="NumberHelper.html#method-i-number_to_percentage" name="method-i-number_to_percentage" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Formats a <code>number</code> as a percentage string (e.g., 65%). You can
customize the format in the <code>options</code> hash.</p>

<h4 id="method-i-number_to_percentage-label-Options">Options</h4>
<ul><li>
<p><code>:locale</code> - Sets the locale to be used for formatting (defaults
to current locale).</p>
</li><li>
<p><code>:precision</code> - Sets the precision of the number (defaults to 3).</p>
</li><li>
<p><code>:significant</code> - If <code>true</code>, precision will be the #
of significant_digits. If <code>false</code>, the # of fractional digits
(defaults to <code>false</code>).</p>
</li><li>
<p><code>:separator</code> - Sets the separator between the fractional and
integer digits (defaults to ".").</p>
</li><li>
<p><code>:delimiter</code> - Sets the thousands delimiter (defaults to "").</p>
</li><li>
<p><code>:strip_insignificant_zeros</code> - If <code>true</code> removes
insignificant zeros after the decimal separator (defaults to
<code>false</code>).</p>
</li><li>
<p><code>:format</code> - Specifies the format of the percentage string The
number field is <code>%n</code> (defaults to "%n%").</p>
</li></ul>

<h4 id="method-i-number_to_percentage-label-Examples">Examples</h4>

<pre class="ruby"><span class="ruby-identifier">number_to_percentage</span>(<span class="ruby-value">100</span>)                                 <span class="ruby-comment"># =&gt; 100.000%</span>
<span class="ruby-identifier">number_to_percentage</span>(<span class="ruby-string">'98'</span>)                                <span class="ruby-comment"># =&gt; 98.000%</span>
<span class="ruby-identifier">number_to_percentage</span>(<span class="ruby-value">100</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">0</span>)                   <span class="ruby-comment"># =&gt; 100%</span>
<span class="ruby-identifier">number_to_percentage</span>(<span class="ruby-value">1000</span>, <span class="ruby-identifier">delimiter</span><span class="ruby-operator">:</span> <span class="ruby-string">'.'</span>, <span class="ruby-identifier">separator</span><span class="ruby-operator">:</span> ,<span class="ruby-string">') # =&gt; 1.000,000%
number_to_percentage(302.24398923423, precision: 5)       # =&gt; 302.24399%
number_to_percentage(1000, locale: :fr)                   # =&gt; 1 000,000%
number_to_percentage('</span><span class="ruby-value">98</span><span class="ruby-identifier">a</span><span class="ruby-string">')                               # =&gt; 98a%
number_to_percentage(100, format: '</span><span class="ruby-operator">%</span><span class="ruby-identifier">n</span>  <span class="ruby-node">%Q)                # =&gt; 100  %
</span></pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-number_to_percentage_source')" id="l_method-i-number_to_percentage_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e772daad20e865297f1907815b44f83de6f41f46/activesupport/lib/active_support/number_helper.rb#L255" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-number_to_percentage_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/number_helper.rb, line 255</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">number_to_percentage</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">number</span>
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>

  <span class="ruby-identifier">defaults</span> = <span class="ruby-identifier">format_options</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:percentage</span>)
  <span class="ruby-identifier">options</span>  = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">options</span>)

  <span class="ruby-identifier">format</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:format</span>] <span class="ruby-operator">||</span> <span class="ruby-string">&quot;%n%&quot;</span>
  <span class="ruby-identifier">format</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-string">'%n'</span>, <span class="ruby-keyword">self</span>.<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span>))
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-number_to_phone">
            
              <b>number_to_phone</b>(number, options = {})
            
            <a href="NumberHelper.html#method-i-number_to_phone" name="method-i-number_to_phone" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Formats a <code>number</code> into a US phone number (e.g., (555)
123-9876). You can customize the format in the <code>options</code> hash.</p>

<h4 id="method-i-number_to_phone-label-Options">Options</h4>
<ul><li>
<p><code>:area_code</code> - Adds parentheses around the area code.</p>
</li><li>
<p><code>:delimiter</code> - Specifies the delimiter to use (defaults to "-").</p>
</li><li>
<p><code>:extension</code> - Specifies an extension to add to the end of the
generated number.</p>
</li><li>
<p><code>:country_code</code> - Sets the country code for the phone number.</p>
</li></ul>

<h4 id="method-i-number_to_phone-label-Examples">Examples</h4>

<pre class="ruby"><span class="ruby-identifier">number_to_phone</span>(<span class="ruby-value">5551234</span>)                                     <span class="ruby-comment"># =&gt; 555-1234</span>
<span class="ruby-identifier">number_to_phone</span>(<span class="ruby-string">'5551234'</span>)                                   <span class="ruby-comment"># =&gt; 555-1234</span>
<span class="ruby-identifier">number_to_phone</span>(<span class="ruby-value">1235551234</span>)                                  <span class="ruby-comment"># =&gt; 123-555-1234</span>
<span class="ruby-identifier">number_to_phone</span>(<span class="ruby-value">1235551234</span>, <span class="ruby-identifier">area_code</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>)                 <span class="ruby-comment"># =&gt; (123) 555-1234</span>
<span class="ruby-identifier">number_to_phone</span>(<span class="ruby-value">1235551234</span>, <span class="ruby-identifier">delimiter</span><span class="ruby-operator">:</span> <span class="ruby-string">' '</span>)                  <span class="ruby-comment"># =&gt; 123 555 1234</span>
<span class="ruby-identifier">number_to_phone</span>(<span class="ruby-value">1235551234</span>, <span class="ruby-identifier">area_code</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>, <span class="ruby-identifier">extension</span><span class="ruby-operator">:</span> <span class="ruby-value">555</span>) <span class="ruby-comment"># =&gt; (123) 555-1234 x 555</span>
<span class="ruby-identifier">number_to_phone</span>(<span class="ruby-value">1235551234</span>, <span class="ruby-identifier">country_code</span><span class="ruby-operator">:</span> <span class="ruby-value">1</span>)                 <span class="ruby-comment"># =&gt; +1-123-555-1234</span>
<span class="ruby-identifier">number_to_phone</span>(<span class="ruby-string">'123a456'</span>)                                   <span class="ruby-comment"># =&gt; 123a456</span>

<span class="ruby-identifier">number_to_phone</span>(<span class="ruby-value">1235551234</span>, <span class="ruby-identifier">country_code</span><span class="ruby-operator">:</span> <span class="ruby-value">1</span>, <span class="ruby-identifier">extension</span><span class="ruby-operator">:</span> <span class="ruby-value">1343</span>, <span class="ruby-identifier">delimiter</span><span class="ruby-operator">:</span> <span class="ruby-string">'.'</span>)
<span class="ruby-comment"># =&gt; +1.123.555.1234 x 1343</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-number_to_phone_source')" id="l_method-i-number_to_phone_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e772daad20e865297f1907815b44f83de6f41f46/activesupport/lib/active_support/number_helper.rb#L139" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-number_to_phone_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/number_helper.rb, line 139</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">number_to_phone</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">number</span>
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>

  <span class="ruby-identifier">number</span>       = <span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">strip</span>
  <span class="ruby-identifier">area_code</span>    = <span class="ruby-identifier">options</span>[<span class="ruby-value">:area_code</span>]
  <span class="ruby-identifier">delimiter</span>    = <span class="ruby-identifier">options</span>[<span class="ruby-value">:delimiter</span>] <span class="ruby-operator">||</span> <span class="ruby-string">&quot;-&quot;</span>
  <span class="ruby-identifier">extension</span>    = <span class="ruby-identifier">options</span>[<span class="ruby-value">:extension</span>]
  <span class="ruby-identifier">country_code</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:country_code</span>]

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">area_code</span>
    <span class="ruby-identifier">number</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp">/(\d{1,3})(\d{3})(\d{4}$)/</span>,<span class="ruby-node">&quot;(\\1) \\2#{delimiter}\\3&quot;</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">number</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp">/(\d{0,3})(\d{3})(\d{4})$/</span>,<span class="ruby-node">&quot;\\1#{delimiter}\\2#{delimiter}\\3&quot;</span>)
    <span class="ruby-identifier">number</span>.<span class="ruby-identifier">slice!</span>(<span class="ruby-number">0</span>, <span class="ruby-number">1</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">number</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-identifier">delimiter</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">delimiter</span>.<span class="ruby-identifier">blank?</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">str</span> = <span class="ruby-string">''</span>
  <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;+#{country_code}#{delimiter}&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">country_code</span>.<span class="ruby-identifier">blank?</span>
  <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">number</span>
  <span class="ruby-identifier">str</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot; x #{extension}&quot;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">extension</span>.<span class="ruby-identifier">blank?</span>
  <span class="ruby-identifier">str</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-number_to_rounded">
            
              <b>number_to_rounded</b>(number, options = {})
            
            <a href="NumberHelper.html#method-i-number_to_rounded" name="method-i-number_to_rounded" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Formats a <code>number</code> with the specified level of
<code>:precision</code> (e.g., 112.32 has a precision of 2 if
<code>:significant</code> is <code>false</code>, and 5 if
<code>:significant</code> is <code>true</code>). You can customize the
format in the <code>options</code> hash.</p>

<h4 id="method-i-number_to_rounded-label-Options">Options</h4>
<ul><li>
<p><code>:locale</code> - Sets the locale to be used for formatting (defaults
to current locale).</p>
</li><li>
<p><code>:precision</code> - Sets the precision of the number (defaults to 3).</p>
</li><li>
<p><code>:significant</code> - If <code>true</code>, precision will be the #
of significant_digits. If <code>false</code>, the # of fractional digits
(defaults to <code>false</code>).</p>
</li><li>
<p><code>:separator</code> - Sets the separator between the fractional and
integer digits (defaults to ".").</p>
</li><li>
<p><code>:delimiter</code> - Sets the thousands delimiter (defaults to "").</p>
</li><li>
<p><code>:strip_insignificant_zeros</code> - If <code>true</code> removes
insignificant zeros after the decimal separator (defaults to
<code>false</code>).</p>
</li></ul>

<h4 id="method-i-number_to_rounded-label-Examples">Examples</h4>

<pre class="ruby"><span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">111.2345</span>)                                  <span class="ruby-comment"># =&gt; 111.235</span>
<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">111.2345</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">2</span>)                    <span class="ruby-comment"># =&gt; 111.23</span>
<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">13</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">5</span>)                          <span class="ruby-comment"># =&gt; 13.00000</span>
<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">389.32314</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">0</span>)                   <span class="ruby-comment"># =&gt; 389</span>
<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">111.2345</span>, <span class="ruby-identifier">significant</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>)               <span class="ruby-comment"># =&gt; 111</span>
<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">111.2345</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">1</span>, <span class="ruby-identifier">significant</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># =&gt; 100</span>
<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">13</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">5</span>, <span class="ruby-identifier">significant</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>)       <span class="ruby-comment"># =&gt; 13.000</span>
<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">111.234</span>, <span class="ruby-identifier">locale</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">fr</span>)                      <span class="ruby-comment"># =&gt; 111,234</span>

<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">13</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">5</span>, <span class="ruby-identifier">significant</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>, <span class="ruby-identifier">strip_insignificant_zeros</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-comment"># =&gt; 13</span>

<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">389.32314</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">4</span>, <span class="ruby-identifier">significant</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># =&gt; 389.3</span>
<span class="ruby-identifier">number_to_rounded</span>(<span class="ruby-value">1111.2345</span>, <span class="ruby-identifier">precision</span><span class="ruby-operator">:</span> <span class="ruby-value">2</span>, <span class="ruby-identifier">separator</span><span class="ruby-operator">:</span> <span class="ruby-string">','</span>, <span class="ruby-identifier">delimiter</span><span class="ruby-operator">:</span> <span class="ruby-string">'.'</span>)
<span class="ruby-comment"># =&gt; 1.111,23</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-number_to_rounded_source')" id="l_method-i-number_to_rounded_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/e772daad20e865297f1907815b44f83de6f41f46/activesupport/lib/active_support/number_helper.rb#L342" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-number_to_rounded_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/number_helper.rb, line 342</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">number_to_rounded</span>(<span class="ruby-identifier">number</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">number</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">valid_float?</span>(<span class="ruby-identifier">number</span>)
  <span class="ruby-identifier">number</span>  = <span class="ruby-constant">Float</span>(<span class="ruby-identifier">number</span>)
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">symbolize_keys</span>

  <span class="ruby-identifier">defaults</span> = <span class="ruby-identifier">format_options</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:locale</span>], <span class="ruby-value">:precision</span>)
  <span class="ruby-identifier">options</span>  = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">merge!</span>(<span class="ruby-identifier">options</span>)

  <span class="ruby-identifier">precision</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span> <span class="ruby-value">:precision</span>
  <span class="ruby-identifier">significant</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span> <span class="ruby-value">:significant</span>
  <span class="ruby-identifier">strip_insignificant_zeros</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span> <span class="ruby-value">:strip_insignificant_zeros</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">significant</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">precision</span> <span class="ruby-operator">&gt;</span> <span class="ruby-number">0</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">number</span> <span class="ruby-operator">==</span> <span class="ruby-number">0</span>
      <span class="ruby-identifier">digits</span>, <span class="ruby-identifier">rounded_number</span> = <span class="ruby-number">1</span>, <span class="ruby-number">0</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">digits</span> = (<span class="ruby-constant">Math</span>.<span class="ruby-identifier">log10</span>(<span class="ruby-identifier">number</span>.<span class="ruby-identifier">abs</span>) <span class="ruby-operator">+</span> <span class="ruby-number">1</span>).<span class="ruby-identifier">floor</span>
      <span class="ruby-identifier">multiplier</span> = <span class="ruby-number">10</span> <span class="ruby-operator">**</span> (<span class="ruby-identifier">digits</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">precision</span>)
      <span class="ruby-identifier">rounded_number</span> = (<span class="ruby-constant">BigDecimal</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">/</span> <span class="ruby-constant">BigDecimal</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">multiplier</span>.<span class="ruby-identifier">to_f</span>.<span class="ruby-identifier">to_s</span>)).<span class="ruby-identifier">round</span>.<span class="ruby-identifier">to_f</span> * <span class="ruby-identifier">multiplier</span>
      <span class="ruby-identifier">digits</span> = (<span class="ruby-constant">Math</span>.<span class="ruby-identifier">log10</span>(<span class="ruby-identifier">rounded_number</span>.<span class="ruby-identifier">abs</span>) <span class="ruby-operator">+</span> <span class="ruby-number">1</span>).<span class="ruby-identifier">floor</span> <span class="ruby-comment"># After rounding, the number of digits may have changed</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">precision</span> <span class="ruby-operator">-=</span> <span class="ruby-identifier">digits</span>
    <span class="ruby-identifier">precision</span> = <span class="ruby-number">0</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">precision</span> <span class="ruby-operator">&lt;</span> <span class="ruby-number">0</span> <span class="ruby-comment"># don't let it be negative</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">rounded_number</span> = <span class="ruby-constant">BigDecimal</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">number</span>.<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">round</span>(<span class="ruby-identifier">precision</span>).<span class="ruby-identifier">to_f</span>
    <span class="ruby-identifier">rounded_number</span> = <span class="ruby-identifier">rounded_number</span>.<span class="ruby-identifier">abs</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">rounded_number</span>.<span class="ruby-identifier">zero?</span> <span class="ruby-comment"># prevent showing negative zeros</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">formatted_number</span> = <span class="ruby-keyword">self</span>.<span class="ruby-identifier">number_to_delimited</span>(<span class="ruby-node">&quot;%01.#{precision}f&quot;</span> <span class="ruby-operator">%</span> <span class="ruby-identifier">rounded_number</span>, <span class="ruby-identifier">options</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">strip_insignificant_zeros</span>
    <span class="ruby-identifier">escaped_separator</span> = <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">escape</span>(<span class="ruby-identifier">options</span>[<span class="ruby-value">:separator</span>])
    <span class="ruby-identifier">formatted_number</span>.<span class="ruby-identifier">sub</span>(<span class="ruby-node">/(#{escaped_separator})(\d*[1-9])?0+\z/</span>, <span class="ruby-string">'\1\2'</span>).<span class="ruby-identifier">sub</span>(<span class="ruby-node">/#{escaped_separator}\z/</span>, <span class="ruby-string">''</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">formatted_number</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    